package com.example.sistemacompras;

import java.util.ArrayList;
import java.util.List;

import com.example.sistemacompras.db.DatabaseHandler;
import com.example.sistemacompras.entity.OrderDetail;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomCartAdapter extends BaseAdapter {

	private Activity activity;
	private List<OrderDetail> data;
	private static LayoutInflater inflater = null;
	public Resources res;
	OrderDetail tempValues = null;
//	int i = 0;
	private DatabaseHandler db;
	

	public CustomCartAdapter(Activity a, List<OrderDetail> mOrderDetails, Resources resLocal) {
		db = new DatabaseHandler(a);
		activity = a;
		data = mOrderDetails;
		res = resLocal;

		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		if (data.size() <= 0)
			return 1;
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public static class ViewHolder {

		public TextView nameTextVIew;
		public TextView qtdTextView;
		public TextView vlextView;
	}

	@Override
	public View getView(int position, final View view, ViewGroup viewGroup) {
		View vi = view;
		ViewHolder holder;

		if (view == null) {

			vi = inflater.inflate(R.layout.cart_item, null);

			holder = new ViewHolder();
			holder.nameTextVIew = (TextView) vi.findViewById(R.id.name);
			holder.qtdTextView = (TextView) vi.findViewById(R.id.qtd);
			holder.vlextView = (TextView) vi.findViewById(R.id.ValorTotal);


			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();

		if (data.size() <= 0) {
			holder.nameTextVIew.setText("No Data");

		} else {

			tempValues = new OrderDetail();
			tempValues = (OrderDetail) data.get(position);

			holder.nameTextVIew.setText(tempValues.getProduct().getName());
			holder.qtdTextView.setText("Qtd: " + String.valueOf(tempValues.getQtd()));
			holder.vlextView.setText("R$ " + tempValues.getProduct().getUnitValue());
//			holder.deleteButton.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					db.deleteOrderDetail(tempValues);
//				}
//			});

			// vi.setOnClickListener(new OnItemClickListener( position ));
		}
		return vi;
	}


}
